<template>
  <div class="user">
    <!--搜索表单-->
    <div class="common-seach-wrap">
      <el-form
        size="small"
        :inline="true"
        :model="formInline"
        class="demo-form-inline"
      >
        <el-form-item label="按区域搜索">
          <el-select  v-model="formInline.province_id" placeholder="省" @change="initCity" clearable>
            <el-option :label="item.name" :value="item.id" v-for="(item,index) in areaList" :key='index'></el-option>
          </el-select>
          <el-select  v-if="formInline.province_id!=''" v-model="formInline.city_id" placeholder="市" @change="initRegion" clearable>
            <el-option :label="item1.name" :value="item1.id" v-for="(item1,index1) in areaList[formInline.province_id]['city']"
                       :key='index1'></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="按商户搜索">
          <el-input
            v-model="formInline.search"
            placeholder="请输入商户名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="onSubmit"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <div class="mb16">
        <div class="common-level-rail fl">
          <el-button size="small" type="primary" icon="el-icon-plus" @click="addClick">添加营业时间</el-button>
        </div>
      </div>
    </div>
    <!--内容-->
    <div class="product-content">
      <div class="table-wrap">
        <el-table size="small" :data="tableData" border style="width: 100%" v-loading="loading">
          <el-table-column prop="shoptime_id" label="ID" width="80"></el-table-column>
          <el-table-column label="配置区域">
            <template slot-scope="scope">
              <div slot="reference" class="name-wrapper" v-if="scope.row.time_range==0">
                <el-tag size="medium">{{ scope.row.region.label }}</el-tag>
              </div>
              <div slot="reference" class="name-wrapper" v-if="scope.row.time_range==1">
                <el-tag type="success" size="medium">商户：{{ scope.row.supplier.name }}</el-tag>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="营业时间">
            <template slot-scope="scope">
              <span>{{ scope.row.start_time }}</span>
              <span style="margin:0 10px">-</span>
              <span>{{ scope.row.end_time }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="create_time" label="创建时间" width="140"></el-table-column>
          <el-table-column fixed="right" label="操作" width="170">
            <template slot-scope="scope">
              <el-button @click="editClick(scope.row)" type="text" size="small" :disabled="scope.row.time_range==1">编辑
              </el-button>
              <el-button @click="deleteClick(scope.row)" type="text" size="small">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!--分页-->
      <div class="pagination">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" background
                       :current-page="curPage" :page-size="pageSize" layout="total, prev, pager, next, jumper"
                       :total="totalDataNumber"></el-pagination>
      </div>
    </div>

    <!--添加-->
    <Add v-if="open_add" :open_add="open_add" :add_type="add_type" :rawData="rawData" :selectModel="selectModel" @closeDialog="closeDialogFunc($event, 'add')"></Add>

    <!--编辑-->
    <Edit v-if="open_edit" :open_edit="open_edit" :rawData="rawData" :selectModel="selectModel" @closeDialog="closeDialogFunc($event, 'edit')"></Edit>

  </div>
</template>

<script>
import Shoptime from '@/api/shoptime.js';
import DataApi from '@/api/data.js';
import Edit from './edit.vue';
import Add from './add.vue';
import { deepClone } from '@/utils/base.js';
export default {
  components: {
    /*编辑组件*/
    Edit: Edit,
    Add: Add
  },
  data() {
    return {
      /*是否加载完成*/
      loading: true,
      /*原始数据*/
      rawData: [],
      /*列表数据*/
      tableData: [],
      /*一页多少条*/
      pageSize: 20,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      /*横向表单数据模型*/
      formInline: {
        search: "",
        province_id: '',
        city_id: '',
        region_id: '',
      },
      /*是否打开添加弹窗*/
      open_add: false,
      /*添加类型*/
      add_type:'',
      /*是否打开编辑弹窗*/
      open_edit: false,
      /*选中的对象*/
      selectModel:{},
      /*当前编辑的对象*/
      userModel: {},
      /*等级*/
      gradeList: [],
      open_tag: false,
      /*所有标签*/
      allTag: [],
      category: [],
    };
  },
  created() {
    //区域
    this.getRegionTableList();
    /*获取列表*/
    this.getTableList();
    this.getRegion()
  },
  methods: {
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.loading = true;
      self.getTableList();
    },

    /*每页多少条*/
    handleSizeChange(val) {
      this.curPage = 1;
      this.pageSize = val;
      this.getTableList();
    },

    /*获取列表*/
    getTableList() {
      let self = this;
      let Params = self.formInline;
      Params.page = self.curPage;
      Params.list_rows = self.pageSize;
      Shoptime.list(Params, true)
        .then(data => {
          self.loading = false;
          self.tableData = data.data.list.data;
          self.totalDataNumber = data.data.list.total;
          self.category = data.data.category;
        })
        .catch(error => {
          self.loading = false;
        });
    },
    /*获取区域列表*/
    getRegionTableList() {
      let self = this;
      DataApi.getRegionTwo({
        treelv:1
      }, true).then(res => {
        self.rawData = res.data.regionData;
      })
      .catch(error => {

      });
    },
    getRegion(){
      let self = this;
      DataApi.getRegion({}, true)
        .then(res => {
          self.areaList = res.data.regionData;
        })
        .catch(error => {

        });
    },
    /*初始化城市id*/
    initCity() {
      this.formInline.city_id = ''
    },
    /*初始化区id*/
    initRegion() {
      this.formInline.region_id = ''
    },

    /*搜索查询*/
    onSubmit() {
      let self = this;
      self.loading = true;
      let Params = self.formInline;
      self.getTableList();
    },
    /*打开添加*/
    addClick(e,type) {
      if(e&&typeof(e)!='undefined'){
        this.add_type=type;
        this.selectModel=deepClone(e);
      }
      this.open_add = true;
    },
    /*打开编辑*/
    editClick(item) {
      console.log(item)
      this.selectModel = item;
      this.open_edit = true;

    },
    /*关闭弹窗*/
    closeDialogFunc(e, f) {
      if (f == 'add') {
        this.open_add = e.openDialog;
        if (e.type == 'success') {
          this.getTableList();
        }
      }
      if (f == 'edit') {
        this.open_edit = e.openDialog;
        if (e.type == 'success') {
          this.getTableList();
        }
      }
    },

    /*删除用户*/
    deleteClick(row) {
      let self = this;
      self
        .$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          self.loading = true;
          Shoptime.delete({
              id: row.shoptime_id
            },
            true
          )
            .then(data => {
              self.loading = false;
              if (data.code == 1) {
                self.$message({
                  message: '恭喜你，删除成功',
                  type: 'success'
                });
                self.getTableList();
              } else {
                self.loading = false;
              }
            })
            .catch(error => {
              self.loading = false;
            });
        })
        .catch(() => {
          self.loading = false;
        });
    }
  }
};
</script>
<style></style>
